﻿using AbpApplication.AppServices;
using AbpApplication.InPut;
using Flurl.Http;
using HandyControl.Controls;
using Newtonsoft.Json;
using NewWms.Extend;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wms_wpf;
using wms_wpf.Apiservice;

namespace NewWms.Apiservice
{
    public class BaseService : IBaseService
    {
        string config = GlobalVariable.Config;
 
        public async Task<string> CreateMaterialBase(InPut_BaseService7 inPut)//物料入库
        {

            
            var url = $"http://{config}/api/BaseService/CreateMaterialBase"; 
            var data = inPut;
            string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
            ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
            if (Response.State)
            {
                Growl.Success(Response.Msg, "msg");
                
                string dto = Response.Dto.ToString();
                return dto;
            }
            else
            {
                MessageBox.Show(Response.Msg);
                return null;
            }
        }
        public async Task<bool> Create_Boxs(InPut_BaseService input)
        {
            try
            {
                var url = $"http://{config}/api/BaseService/Create_Boxs";
                var data = input;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    MessageBox.Show(Response.Msg);
                    return Response.State;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("Create_Boxs", ex);
                return false;
            }

        }//加盒子
        public async Task<bool> Create_Warehouses(InPut_BaseService3 input)//配库位
        {
            try
            {
                var url = $"http://{config}/api/BaseService/Create_Warehouses";
                var data = input;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    MessageBox.Show(Response.Msg);
                    return Response.State;
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("Create_Warehouses", ex);
                return false;
            }
        }
        public async Task<bool> DeleteBox(InPut_BaseService8 InPut)
        {
            try
            {
                var url = $"http://{config}/api/BaseService/DeleteBox";
                var data = InPut;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    MessageBox.Show(Response.Msg);
                    return Response.State;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("DeleteBox", ex);
                return false;
            }
        }//删盒子
        public async Task<bool> CreateTaskOrder(InPut_BaseService6 InPut)//添加agv入库任务
        {
            try
            {
                var url = $"http://{config}/api/BaseService/CreateTaskOrder";
                var data = InPut;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    MessageBox.Show(Response.Msg);
                    return Response.State;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("CreateTaskOrder", ex);
                return false;
            }
        }
        public async Task<bool> DeleteTaskOrder(List<string> InPut)//删除agv任务
        {
            try
            {
                var url = $"http://{config}/api/BaseService/DeleteTaskOrder";
                var data = InPut;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    MessageBox.Show(Response.Msg);
                    return Response.State;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("DeleteTaskOrder", ex);
                return false;
            }
        }
        public async Task<bool> RemovesMaterial(List<string> InPut)//删料
        {
            try
            {
                var url = $"http://{config}/api/BaseService/RemovesMaterial";
                var data = InPut;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    MessageBox.Show(Response.Msg);
                    return Response.State;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("RemovesMaterial", ex);
                return false;
            }
        }
        public async Task<bool> DeleteMaterial(InPut_BaseService4 InPut)//出料
        {
            try
            {
                var url = $"http://{config}/api/BaseService/DeleteMaterial";
                var data = InPut;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    MessageBox.Show(Response.Msg);
                    return Response.State;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("DeleteMaterial", ex);
                return false;
            }
        }
        public async Task<string> UpdateTaskOrderState(InPut_BaseService11 InPut)//更新任务状态
        {
            try
            {
                var url = $"http://{config}/api/BaseService/UpdateTaskOrderState";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    
                    return Response.Msg;
                }
                else
                {
                    Growl.Info(Response.Msg, "msg");
                    return Response.Msg;
                }

            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("任务状态修改错误:UpdateTaskOrderState", ex);
                return null;
            }
        }
        public async Task<string> CreateLog(string Type, string Note, string? Definition,
        string? MaterialCode, string? MaterialName, string? Batch, string? Size,
        string? OrderNo, string? BoxCode, string? WarehouseLineCode)//生成日志
        {

            try
            {


                InPut_BaseService15 InPut = new InPut_BaseService15();
                InPut.Type = Type;
                InPut.Note = Note;
                InPut.User = Jwt.UserName;
                InPut.Definition = Definition;
                InPut.MaterialCode = MaterialCode;
                InPut.MaterialName = MaterialName;
                InPut.Batch = Batch;
                InPut.Size = Size;
                InPut.OrderNo = OrderNo;
                InPut.BoxCode = BoxCode;
                InPut.WarehouseLineCode=WarehouseLineCode;
                var url = $"http://{config}/api/BaseService/CreateLog";
                var data = InPut;
                string result = await url.PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {

                    return Response.Msg;
                }
                else
                {
                    return Response.Msg;
                }
            }
            catch (Exception ex)
            {
                Growl.Error("接口调用失败", "msg");
                LognetHelper.WriteLog("CreateLog", ex);
                return null;
            }
        }
        public async Task<bool> CreateTaskOrderOut(InPut_BaseService13 InPut)//添加agv出库任务
        {
            try
            {
                var url = $"http://{config}/api/BaseService/CreateTaskOrderOut";
                var data = InPut;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    MessageBox.Show(Response.Msg);
                    return Response.State;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("CreateTaskOrderOut", ex);
                return false;
            }
        }
        public async Task<bool> CreateNeetOutMaterial(InPut_BaseService10 InPut)//手动出库-添加需出料-添加agv任务
        {
            try
            {
                var url = $"http://{config}/api/BaseService/CreateNeetOutMaterial";
                var data = InPut;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    MessageBox.Show(Response.Msg);
                    return Response.State;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("CreateNeetOutMaterial", ex);
                return false;
            }
        }
        public async Task<bool> Update_Box(InPut_BaseService2 input)//更新箱盒
        {
            try
            {
                var url = $"http://{config}/api/BaseService/Update_Box";
                var data = input;
                string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
                ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
                if (Response.State)
                {
                    Growl.Success(Response.Msg, "msg");
                    return Response.State;
                }
                else
                {
                    MessageBox.Show(Response.Msg);
                    return Response.State;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                LognetHelper.WriteLog("更新箱盒接口失败:Update_Box", ex);
                return false;
            }
        }
        public async Task<bool> UpdateMaterialBaserQuantity(InPut_BaseService7 inPut)
        {
            var url = $"http://{config}/api/BaseService/UpdateMaterialBaserQuantity";
            var data = inPut;
            string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
            ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
            if (Response.State)
            {
                Growl.Success(Response.Msg, "msg");

         
                return true;
            }
            else
            {
                MessageBox.Show(Response.Msg);
                return false;
            }
        }
        public async Task<bool> CreateMaterialProperty(InPut_BaseService14 InPut)//添加基础物料
        {
            var url = $"http://{config}/api/BaseService/CreateMaterialProperty";
            var data = InPut;
            string result = await url.WithHeader("Authorization", " Bearer" + " " + Jwt.JwtKey).PostJsonAsync(data).ReceiveString();
            ApiResponse Response = JsonConvert.DeserializeObject<ApiResponse>(result);
            if (Response.State)
            {
                Growl.Success(Response.Msg, "msg");


                return true;
            }
            else
            {
                MessageBox.Show(Response.Msg);
                return false;
            }
        }
    }
}
